package cn.itcast.solr;

import cn.itcast.pojo.Heiheima;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.SolrParams;
import org.junit.Test;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SolrIndexSupperQueryTest {

    //高级查询
    //高亮
    @Test
    public void testHighLighter() throws SolrServerException {
        SolrServer server=new HttpSolrServer("http://localhost:8080/solr/core2");

        //查询对象
        SolrQuery query=new SolrQuery("title:手机" );
        //添加高亮字段
        query.addHighlightField("title");
        query.setHighlightSimplePre("<em>");
        query.setHighlightSimplePost("</em>");
        //执行solr的查询请求
        //获取响应结果
        QueryResponse queryResponse = server.query(query);
        /**
         * 获取高亮结果数据
         * 第一层map的key为高亮文档的id
         * 第二层map的key为高亮的字段名称,value就是高亮的结果
         * 由于可能存在多值的情况,所以高亮结果返回使用集合来接收
         */
        Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
//        Set<String> keySet = highlighting.keySet();
//        System.out.println(highlighting.get("16"));
        List<Heiheima> list = queryResponse.getBeans(Heiheima.class);
        System.out.println("一共查到:"+list.size()+"条");
        System.out.println("=======================");
        for (Heiheima pojo : list) {
            String id = pojo.getId();
            String highlightTitle = highlighting.get(id).get("title").get(0);
            Long price = pojo.getPrice();
            System.out.println("id:"+id+" title:"+highlightTitle+" price:"+price);
        }
    }

    //排序
    @Test
    public void testSortQuery() throws SolrServerException {
        SolrServer server=new HttpSolrServer("http://localhost:8080/solr/core2");

        //查询对象
        SolrQuery query=new SolrQuery("title:手机" );
        //添加排序
        query.setSort("price", SolrQuery.ORDER.desc);
        //执行solr的查询请求
        //获取响应结果
        QueryResponse queryResponse = server.query(query);

        List<Heiheima> list = queryResponse.getBeans(Heiheima.class);
        System.out.println("一共查到:"+list.size()+"条");
        System.out.println("=======================");
        for (Heiheima pojo : list) {
            String id = pojo.getId();
            String title = pojo.getTitle();
            Long price = pojo.getPrice();
            System.out.println("id:"+id+" title:"+title+" price:"+price);
        }
    }

    //分页
    @Test
    public void testPageQuery() throws SolrServerException {
        //准备分页参数
        int currentPage=2; //当前页
        final int PAGE_SIZE=5; //每页显示条数
        int start=(currentPage-1)*PAGE_SIZE;//当前页的起始条数
        SolrServer server=new HttpSolrServer("http://localhost:8080/solr/core2");

        //查询对象
        SolrQuery query=new SolrQuery("title:手机" );
        //设置分页信息到查询对象中去
        query.setStart(start);
        query.setRows(5);
        //执行solr的查询请求
        //获取响应结果
        QueryResponse queryResponse = server.query(query);

        List<Heiheima> list = queryResponse.getBeans(Heiheima.class);
        System.out.println("一共查到:"+list.size()+"条");
        System.out.println("=======================");
        for (Heiheima pojo : list) {
            String id = pojo.getId();
            String title = pojo.getTitle();
            Long price = pojo.getPrice();
            System.out.println("id:"+id+" title:"+title+" price:"+price);
        }
    }

    //删除
    @Test
    public void testDeleteQuery() throws SolrServerException, IOException {
        SolrServer solrServer=new HttpSolrServer("http://localhost:8080/solr/core2");
//        solrServer.deleteById("1");
        solrServer.deleteByQuery("title:手机");
        solrServer.commit();
    }
}
